# -*- coding: utf-8 -*-
"""
date: Wed Jan  2 15:01:35 2019
python: Anaconda 3.6.5
author: kanade
email: kanade@blisst.cn
"""
import numpy as np
import matplotlib.pyplot as mp


# 训练数据
x = np.array([0.5, 0.6, 0.8, 1.1, 1.4])
y = np.array([5.0, 5.5, 6.0, 6.8, 7.0])
# 深度，即重复次数
depth = 1000
# 学习率，一般介于(0, 1)之间, learning rate
lrate = 0.01
# 记录过程的损失值
loss = None
# 记录过程中的k，b,顺便设置起始值为(1,1)
k, b = 1, 1

for i in range(1, depth+1):
    loss = (((y - (k * x + b)) ** 2).sum() / 2)
    print('第%d次：k=%.8f,b=%.8f,loss=%.8f'%(i, k, b, loss))
    # 对b的偏微分
    db = -(y - (k * x + b)).sum()
    # 对k的偏微分
    dk = -((y - (k * x + b)) * x).sum()
    # 移动一部分距离
    k = k - lrate * dk
    b = b - lrate * db

# 画出训练数据的点
mp.scatter(x, y, marker='s', c='dodgerblue', alpha=0.5, 
           s=50, label='Test')
# 画出预测数据的点
mp.scatter(x, k * x + b, marker='D', c='orangered', alpha=0.5, 
           s=50, label='Predict')
# 画预测函数
mp.plot(x, k * x + b)
mp.legend()

mp.show()













